package com.example.iot.entity.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.example.iot.entity.RuleNode;
import com.example.iot.entity.id.RuleChainId;
import com.example.iot.entity.id.RuleNodeId;
import com.example.iot.mapper.mysql.handlers.UUIDTypeHandler;
import com.example.iot.util.UUIDUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 
 * </p>
 *
 * @author ChenXiangpeng
 * @since 2021-02-25
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "rule_node", autoResultMap = true)
public class RuleNodePO extends BasePO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 规则链id
     */
    @TableField(typeHandler = UUIDTypeHandler.class)
    private UUID ruleChainId;

    /**
     * x轴位置
     */
    private String positionX;

    /**
     * y轴位置
     */
    private String positionY;

    /**
     * 配置
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private JsonNode configuration;

    /**
     * 节点类型
     */
    private String type;

    /**
     * 节点名称
     */
    private String name;

    public static final String ID = "id";

    public static final String RULE_CHAIN_ID = "rule_chain_id";

    public static final String POSITION_X = "position_x";

    public static final String POSITION_Y = "position_y";

    public static final String CONFIGURATION = "configuration";

    public static final String TYPE = "type";

    public static final String NAME = "name";

    public RuleNode toDto(){
        RuleNode ruleNode = new RuleNode();
        ruleNode.setId(new RuleNodeId(UUIDUtil.fromString(this.getUuid())));
        ruleNode.setName(this.getName());
        ruleNode.setType(this.getType());
        ruleNode.setRuleChainId(new RuleChainId(this.getRuleChainId()));
        ruleNode.setConfiguration(this.getConfiguration());
        return ruleNode;
    }

    public static RuleNodePO ofDto(RuleNode dto){
        RuleNodePO ruleNodePO = new RuleNodePO();
        ruleNodePO.setUuid(Objects.isNull(dto.getId()) ? null : dto.getId().getIdStr());
        ruleNodePO.setConfiguration(dto.getConfiguration());
        ruleNodePO.setName(dto.getName());
        ruleNodePO.setRuleChainId(dto.getRuleChainId().getId());
        ruleNodePO.setPositionX(dto.getPositionX());
        ruleNodePO.setPositionY(dto.getPositionY());
        ruleNodePO.setType(dto.getType());
        return ruleNodePO;
    }
}
